<?php

class cmsControlPage extends commonControlPage 
{
	//入口	
    public function managerModule() 
	{
        $act = $_REQUEST['act'] ? $_REQUEST['act'] : 'list';
        call_user_func(array($this,$act.'Action'));
    }

	/*[文章列表]*/
	private function listAction() 
	{
		$keyword = $_REQUEST['keyword'];
		$this->view->article_list = cmsEntity::GetList($keyword);
		$this->view->list_act=' class="active" '; 
        $this->showMainPage('admincp/cms_list');
    }

	/*[添加文章]*/
    private function addAction() 
	{
        if (POST && AJAX) 
		{
            $data = $this->articleValidate();
            $_ENV['db']->runSql("INSERT INTO `article` ".$this->buildSql(array(
                    'title' => $data['title'],
                    'content' => $data['desc'],
                    'cid' => $data['cid'],
                    'author_id' => $this->user['uid'],
                    'author_name' => $this->user['username'],
                    'created_time' => $_ENV['time'],
                    'last_time' => $_ENV['time'],
					'description' => $data['description'],
                    'sort' => $data['sort']
                )));
            $this->outputJson(1,'操作成功');
        }
        $this->view->addScript('editor/kindeditor,editor/zh_CN,cselector');
		$this->view->article_class = BindComboxs('cid','cid',cmsEntity::GetArticleClass(),'class_id','class_name');
		$this->view->add_act=' class="active" '; 
		$this->view->act='add';
		$this->showMainPage('admincp/cms_modi');
    }
	
	/*[编辑文章]*/
    private function modiAction() 
	{
        $article_id = $_GET['article_id'];
        $article    = cmsEntity:: GetRow( $article_id);
        if (!$article) $this->showMessage('指定的文章不存在',AJAX?'0':'back');
		
        if (POST && AJAX) 
		{
            $data = $this->articleValidate();
            $_ENV['db']->runSql("UPDATE `article` ".$this->buildSql(array(
                    'title' => $data['title'],
                    'content' => $data['desc'],
                    'cid' => $data['cid'],
                    'author_id' => $this->user['uid'],
                    'author_name' => $this->user['username'],
                    'created_time' => $_ENV['time'],
                    'last_time' => $_ENV['time'],
					'description' => $data['description'],
                    'sort' => $data['sort']
                )).' WHERE article_id = '.$article_id);
            $this->outputJson(1,'操作成功');
        }

        $this->view->addScript('editor/kindeditor,editor/zh_CN,cselector');
        $this->view->article = $article;
		$this->view->article_class = BindComboxs('cid','cid',cmsEntity::GetArticleClass(),'class_id','class_name',$article['cid']);
        $class_path = $_ENV['db']->fetchCol("SELECT path FROM article_class where class_id = '{$article['cid']}'");
        
		if($class_path)
		{
			$class_path = $class_path.',';
		}
		$this->view->class_names = $_ENV['db']->fetchAll("SELECT class_name FROM article_class WHERE `class_id` IN ({$class_path}{$article['cid']})");
		$this->view->edit_act=' class="active" '; 
		$this->view->act='modi';
        $this->showMainPage('admincp/cms_modi');
    }
	
	/*[提交数据验证]*/
	private function articleValidate() {

        $data = getRequests(array('act','cid','title','description','sort'));

        $title_length = mb_strlen($data['title'],'utf-8');
        if (!$title_length || $title_length > 200) {
            $this->outputJson(0,'文章标题不能超过200个字符');
        }
        
		$data['sort'] = !$data['sort']?0:$data['sort'];
		if ($data['sort'] && !preg_match('/^[0-9]+$/',$data['sort'])) 
		{
            $this->outputJson(0,'排序必须为数字');
        } 
		
        if ($data['cid']) 
		{
            $class_id = $_ENV['db']->fetchCol("SELECT class_id FROM `article_class` WHERE `class_id` = '{$data['cid']}'");
            if (!$class_id) 
			{
                $this->outputJson(-400,'文章分类不存在');
            }
            $next_class = $_ENV['db']->fetchCol('SELECT class_id FROM `article_class` WHERE `parent_id` = \''.$data['cid'].'\'');
            if ($next_class) 
			{
                $this->outputJson(-400,'文章分类必须为最低级分类');
            }
        }
        $data['desc'] = htmlspecialchars($_POST['desc']);
        return $data;
    }
	
	 /*[删除文章]*/
	public function deleteModule() {
        if(AJAX) {
            $uid = (int)$_GET['uid'];
            $_ENV['db']->runSql("DELETE FROM `article` WHERE `article_id` = '{$uid}'");
            $this->outputJson(1,'操作成功');
        } else {
            $this->showMessage('Bad request');
        }
    }
	
	
    private function uploadAction() {
        $upload = new class_upload();
        $upload->setAllowExt('image');
        $filepath = 'article/'.date('Y/m').'/'.substr(md5(uniqid(1)),10);
        if (!$upload->upload('imgFile',ATTACH_PATH.$filepath)) {
            $message = $upload->getError();
            $error = 1;
            echo json_encode(array('message'=>$message,'error'=>$error));
        } else {
            $url = "./attach/{$filepath}.".$upload->getFileExt();
            $error = 0;
            echo json_encode(array('url'=>$url,'error'=>$error));
        }
    } 
}